package com.profgj.profgj.service.impl;

import com.profgj.profgj.dao.ProductMonthEnergyconsumptionDao;
import com.profgj.profgj.entity.ProjectTotalEnergyconsumption;
import com.profgj.profgj.dao.ProjectTotalEnergyconsumptionDao;
import com.profgj.profgj.enums.CalculatedValue;
import com.profgj.profgj.service.ProjectTotalEnergyconsumptionService;
import com.profgj.profgj.utils.EmptyUtils;
import com.profgj.profgj.vo.ProductMonthEnergyconsumptionVo;
import com.profgj.profgj.vo.ProjectTotalConsumer;
import com.profgj.profgj.vo.ProjectTotalInfoResult;
import com.profgj.profgj.vo.ProjectTotalInfoVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (ProjectTotalEnergyconsumption)表服务实现类
 *
 * @author makejava
 * @since 2021-07-05 10:38:07
 */
@Service("projectTotalEnergyconsumptionService")
public class ProjectTotalEnergyconsumptionServiceImpl implements ProjectTotalEnergyconsumptionService {
    @Resource
    private ProjectTotalEnergyconsumptionDao projectTotalEnergyconsumptionDao;

    @Resource
    private ProductMonthEnergyconsumptionDao productMonthEnergyconsumptionDao;


    /**
     * 取当前项目下的所有产品的 每个项的 月消耗量之和
     * 在计算项目总能耗 和 能耗替代量 十三五  十四五 全年
     * @param projectId     要计算项目的id
     * @param beforeYear     前年的十三五入统能耗的 年号
     * @param currentYear       当前年的十四五入统能耗的 年号
     * @return
     */
    @Override
    public boolean projectTotalEnergyConsumption(Long projectId,Integer beforeYear, Integer currentYear){

        // 创建项目的总能耗量
        ProjectTotalEnergyconsumption projectTotalEnergyconsumption = new ProjectTotalEnergyconsumption();
        // id查询该项目下的所有产品的月 各个消耗量的项之和
        ProjectTotalConsumer projectTotalConsumer = this.projectTotalEnergyconsumptionDao.projectTotalConsumer(projectId);
        
        // 计算能耗之合
        Double energyconsumptionSum = energyconsumptionSum(projectTotalConsumer);
        // 设置关联项目的id
        projectTotalEnergyconsumption.setProjectBaseInfoId(projectTotalConsumer.getProjectId());
        // 设置项目总能耗
        projectTotalEnergyconsumption.setProjectTotalEnergyconsumption(energyconsumptionSum);
        // 设置项目能耗替代量
        projectTotalEnergyconsumption.setEnergyconsumptionReplacement(energyconsumptionSum);
        
        //  2020年全年月能耗list
        List<ProductMonthEnergyconsumptionVo> productMonthEnergyconsumptionVoList_before = this.productMonthEnergyconsumptionDao.projectMonthConsumerList(projectId, beforeYear);

        if (productMonthEnergyconsumptionVoList_before == null || productMonthEnergyconsumptionVoList_before.size() == 0) {
            return false;
        }

        // 将产品月能耗全年记录进行和运算
        ProjectTotalConsumer before_year_projectTotalConsumer = new ProjectTotalConsumer();
        long befor_productMonthElectricitySum = 0;
        double befor_productMonthCoalSum = 0;
        double befor_productMonthNaturalgasSum = 0;
        double befor_productMonthFueloilSum = 0;
        double befor_productMonthWaterSum = 0;
        int userId = 0;
        for(ProductMonthEnergyconsumptionVo productMonthEnergyconsumptionVo :productMonthEnergyconsumptionVoList_before) {
            // 算出每个能耗的总和
            befor_productMonthElectricitySum += productMonthEnergyconsumptionVo.getProductMonthElectricity();
            befor_productMonthCoalSum += productMonthEnergyconsumptionVo.getProductMonthCoal();
            befor_productMonthNaturalgasSum += productMonthEnergyconsumptionVo.getProductMonthNaturalgas();
            befor_productMonthFueloilSum += productMonthEnergyconsumptionVo.getProductMonthFueloil();
            befor_productMonthWaterSum += productMonthEnergyconsumptionVo.getProductMonthWater();
            // 附带设置上企业字段和项目名称和id
            before_year_projectTotalConsumer.setEnName(productMonthEnergyconsumptionVo.getEnName());
            before_year_projectTotalConsumer.setProjectId(productMonthEnergyconsumptionVo.getProjectId());
            before_year_projectTotalConsumer.setProjectName(productMonthEnergyconsumptionVo.getProjectName());
            before_year_projectTotalConsumer.setProjectName(productMonthEnergyconsumptionVo.getProjectName());
            userId = productMonthEnergyconsumptionVo.getEnUserId();
        }
        // 将各项能耗总和 设置到前年的能耗对象(2020全年能耗)
        before_year_projectTotalConsumer.setSumMonthElectricity(befor_productMonthElectricitySum);
        before_year_projectTotalConsumer.setSumMonthCoal(befor_productMonthCoalSum);
        before_year_projectTotalConsumer.setSumMonthNaturalgas(befor_productMonthNaturalgasSum);
        before_year_projectTotalConsumer.setSumMonthFueloil(befor_productMonthFueloilSum);
        before_year_projectTotalConsumer.setSumMonthWater(befor_productMonthWaterSum);

        // 计算2020所有能耗之合
        Double before_year_energyconsumptionSum = energyconsumptionSum(before_year_projectTotalConsumer);
        // 十三五入统能耗 全年2020年
        projectTotalEnergyconsumption.setTenThreeFiveEnergyconsumption(before_year_energyconsumptionSum);
        
        

        //  2021年全年月能耗list
        List<ProductMonthEnergyconsumptionVo> productMonthEnergyconsumptionVoList_current = this.productMonthEnergyconsumptionDao.projectMonthConsumerList(projectId, currentYear);

        // 将产品月能耗当前全年记录进行和运算
        ProjectTotalConsumer current_year_projectTotalConsumer = new ProjectTotalConsumer();
        long current_productMonthElectricitySum = 0;
        double current_productMonthCoalSum = 0;
        double current_productMonthNaturalgasSum = 0;
        double current_productMonthFueloilSum = 0;
        double current_productMonthWaterSum = 0;
        for(ProductMonthEnergyconsumptionVo productMonthEnergyconsumptionVo :productMonthEnergyconsumptionVoList_current) {
            // 算出每个能耗的总和
            current_productMonthElectricitySum += productMonthEnergyconsumptionVo.getProductMonthElectricity();
            current_productMonthCoalSum += productMonthEnergyconsumptionVo.getProductMonthCoal();
            current_productMonthNaturalgasSum += productMonthEnergyconsumptionVo.getProductMonthNaturalgas();
            current_productMonthFueloilSum += productMonthEnergyconsumptionVo.getProductMonthFueloil();
            current_productMonthWaterSum += productMonthEnergyconsumptionVo.getProductMonthWater();
            // 附带设置上企业字段和项目名称和id
            current_year_projectTotalConsumer.setEnName(productMonthEnergyconsumptionVo.getEnName());
            current_year_projectTotalConsumer.setProjectId(productMonthEnergyconsumptionVo.getProjectId());
            current_year_projectTotalConsumer.setProjectName(productMonthEnergyconsumptionVo.getProjectName());

        }
        // 将各项能耗总和 设置到当前年的能耗对象(2021全年能耗)
        current_year_projectTotalConsumer.setSumMonthElectricity(current_productMonthElectricitySum);
        current_year_projectTotalConsumer.setSumMonthCoal(current_productMonthCoalSum);
        current_year_projectTotalConsumer.setSumMonthNaturalgas(current_productMonthNaturalgasSum);
        current_year_projectTotalConsumer.setSumMonthFueloil(current_productMonthFueloilSum);
        current_year_projectTotalConsumer.setSumMonthWater(current_productMonthWaterSum);


        // 计算当前年能耗之合(2021年全年)
        Double current_year_energyconsumptionSum = energyconsumptionSum(current_year_projectTotalConsumer);
        // 十四五新增能耗 预计2021年
        projectTotalEnergyconsumption.setTenFourFiveEnergyconsumption(current_year_energyconsumptionSum);

        projectTotalEnergyconsumption.setEnUserId(userId);
        projectTotalEnergyconsumption.setDeleted(0);

        // 先project_base_info_id查询项目id是否存在
        ProjectTotalEnergyconsumption projectTotalEnergyconsumption_q = projectTotalEnergyconsumptionDao.queryByProject_Base_Info_id(projectId);

        int change = 0;
        if (!EmptyUtils.isProductTotalEnergyconsumptionListEmpty(projectTotalEnergyconsumption_q)) {
            // 不存在则添加
            change = projectTotalEnergyconsumptionDao.insert(projectTotalEnergyconsumption);
        } else {
            // 存在则更新
            projectTotalEnergyconsumption_q.setProjectTotalEnergyconsumption(projectTotalEnergyconsumption.getProjectTotalEnergyconsumption());
            projectTotalEnergyconsumption_q.setEnergyconsumptionReplacement(projectTotalEnergyconsumption.getEnergyconsumptionReplacement());
            projectTotalEnergyconsumption_q.setTenThreeFiveEnergyconsumption(projectTotalEnergyconsumption.getTenThreeFiveEnergyconsumption());
            projectTotalEnergyconsumption_q.setTenFourFiveEnergyconsumption(projectTotalEnergyconsumption.getTenFourFiveEnergyconsumption());
            change = projectTotalEnergyconsumptionDao.update(projectTotalEnergyconsumption_q);
        }

        // 有记录更变大于0 返回真 否则返回 假
        return change >0 ? true : false;
    }

    /**
     * 计算能耗之和
     * @param projectTotalConsumer
     * @return
     */
    private Double energyconsumptionSum(ProjectTotalConsumer projectTotalConsumer) {
        // 计算所有能耗之和
    
                // 算出煤炭总能耗(tce标准煤)
        return CalculatedValue.coalConvStandardCoal(projectTotalConsumer.getSumMonthCoal()) +
                        // 算出电力总能耗(tce标准煤)
                        CalculatedValue.electricityConvStandardCoal(projectTotalConsumer.getSumMonthElectricity()) +
                        // 算出天然气总能耗(tce标准煤)
                        CalculatedValue.naturalgasConvStandardCoal(projectTotalConsumer.getSumMonthNaturalgas()) +
                        // 算出燃料油总能耗(tce标准煤)
                        CalculatedValue.fueloilConvStandardCoal(projectTotalConsumer.getSumMonthFueloil()) +
                        // 算出水总能耗(tce标准煤)
                        CalculatedValue.waterConvStandardCoal(projectTotalConsumer.getSumMonthWater());
    }
    
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ProjectTotalEnergyconsumption queryById(Long id) {
        return this.projectTotalEnergyconsumptionDao.queryById(id);
    }


    /**
     *  分页web页面项目总能耗展示查询
     *
     * @param offset    查询的起始
     * @param limit     查询条数
     * @return
     */
    @Override
    public List<ProjectTotalInfoVo> queryProjectTotalInfoByLimit(Integer offset, Integer limit) {
        return this.projectTotalEnergyconsumptionDao.queryProjectTotalInfoByLimit(offset, limit);
    }

    /**
     *  使用关联用户id 分页web页面项目总能耗展示查询
     *
     * @param offset    查询的起始
     * @param limit     查询条数
     * @return
     */
    @Override
    public List<ProjectTotalInfoVo> queryProjectTotalInfoByLimitAndUserId(Integer offset, Integer limit,Integer userId){
        return this.projectTotalEnergyconsumptionDao.queryProjectTotalInfoByLimitAndUserId(offset, limit,userId);
    }

    /**
     * 对分页web页面项目总能耗展示查询的list列进行 sum统计
     * @param offset
     * @param limit
     * @return
     */
    @Override
    public ProjectTotalInfoResult queryProjectTotalInfoByLimitSumResult(Integer offset, Integer limit){
        return this.projectTotalEnergyconsumptionDao.queryProjectTotalInfoByLimitSumResult(offset, limit);
    }


    /**
     * 对分页web页面项目总能耗展示查询的list列进行 sum统计
     * @param offset
     * @param limit
     * @return
     */
    @Override
    public ProjectTotalInfoResult queryProjectTotalInfoByLimitSumResultAndUserId(Integer offset, Integer limit,Integer userId){
        return this.projectTotalEnergyconsumptionDao.queryProjectTotalInfoByLimitSumResultAndUserId(offset, limit,userId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ProjectTotalEnergyconsumption> queryAllByLimit(int offset, int limit) {
        return this.projectTotalEnergyconsumptionDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param projectTotalEnergyconsumption 实例对象
     * @return 实例对象
     */
    @Override
    public ProjectTotalEnergyconsumption insert(ProjectTotalEnergyconsumption projectTotalEnergyconsumption) {
        this.projectTotalEnergyconsumptionDao.insert(projectTotalEnergyconsumption);
        return projectTotalEnergyconsumption;
    }

    /**
     * 修改数据
     *
     * @param projectTotalEnergyconsumption 实例对象
     * @return 实例对象
     */
    @Override
    public ProjectTotalEnergyconsumption update(ProjectTotalEnergyconsumption projectTotalEnergyconsumption) {
        this.projectTotalEnergyconsumptionDao.update(projectTotalEnergyconsumption);
        return this.queryById(projectTotalEnergyconsumption.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.projectTotalEnergyconsumptionDao.deleteById(id) > 0;
    }

    /**
     * 关键字和用户id 查询项目总能耗分页
     * @param search
     * @param start
     * @param length
     * @param userId
     * @return
     */
    @Override
    public List<ProjectTotalInfoVo> queryProjectTotalInfoByKeywordLimitAndUserId(String search, Integer start, Integer length, Integer userId){
        return this.projectTotalEnergyconsumptionDao.queryProjectTotalInfoByKeywordLimitAndUserId(search, start, length, userId);
    }

    /**
     * 关键字 查询项目总能耗分页
     * @param search
     * @param start
     * @param length
     * @return
     */
    @Override
    public List<ProjectTotalInfoVo> queryProjectTotalInfoByKeywordLimit(String search, Integer start, Integer length) {
        return this.projectTotalEnergyconsumptionDao.queryProjectTotalInfoByKeywordLimit(search, start, length);
    }

    /**
     * 关键字和用户id查询项目总能耗
     * @param search
     * @param userId
     * @return
     */
    @Override
    public List<ProjectTotalInfoVo> queryProjectTotalInfoByKeywordAndUserId(String search, Integer userId){
        return this.projectTotalEnergyconsumptionDao.queryProjectTotalInfoByKeywordAndUserId(search, userId);
    }

    /**
     * 用户id查询项目总能耗
     * @param userId
     * @return
     */
    @Override
    public List<ProjectTotalInfoVo> queryProjectTotalInfoByUserId(Integer userId){
        return this.projectTotalEnergyconsumptionDao.queryProjectTotalInfoByUserId(userId);
    }

    /**
     * 关键字查询项目总能耗
     * @param search
     * @return
     */
    @Override
    public List<ProjectTotalInfoVo> queryProjectTotalInfoByKeyword(String search){
        return this.projectTotalEnergyconsumptionDao.queryProjectTotalInfoByKeyword(search);
    }

    /**
     * 查询项目总能耗
     * @return
     */
    @Override
    public List<ProjectTotalInfoVo> queryProjectTotalInfoAll(){
        return this.projectTotalEnergyconsumptionDao.queryProjectTotalInfoAll();
    }

    /**
     * 根据产品id查询总能耗信息
     * @param id
     * @return
     */
    @Override
    public ProjectTotalEnergyconsumption queryProjectTotalInfoByProjectBaseInfoId(Long id){
        return this.projectTotalEnergyconsumptionDao.queryProjectTotalInfoByProjectBaseInfoId(id);
    }
}
